<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\CallBase;
use App\Model\VoiceCall;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class VoiceCallController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('话单详情');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(VoiceCall::class, function (Grid $grid) {
            $where = [
                // ['status', VoiceCall::STATUS_OK]
            ];
            $grid->model()->where($where)->orderBy('id', 'desc');
            $grid->id('话单ID');
            $grid->from('话单类型')->display(function ($data) {
                return VoiceCall::FROM_TYPE[$data];
            });
            $grid->call_mark('呼叫类型')->display(function ($data) {
                return $data == 2 ? '女用户呼叫' : '';
            });
            $grid->status('状态')->display(function ($data) {
                return CallBase::STATUS[$data] ?? '-';
            });
            $grid->channel_id('频道ID');
            $grid->fromUser()->user_profile_id('拨打方ID');
            $grid->fromUser()->nick_name('拨打方昵称');
            $grid->toUser()->user_profile_id('接听方ID');
            $grid->toUser()->nick_name('接听方昵称');
            $grid->coin_setting('价格(金币/分钟)');
            $grid->start_time('发起时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', $time);
            });
            $grid->answer_time('接听时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', $time);
            });
            $grid->end_time('结束时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', $time);
            });
            $grid->call_time('通话时长')->display(function ($time) {
                return $time < 10 ? 0 : ceil($time / 60) . '分钟';
            });
            $grid->total_cost('话单费用')->display(function ($data) {
                return "{$data}";
            });
            $grid->caller_cost('拨打方扣除')->display(function ($data) {
                return "{$data}";
            });
            $grid->host_gain('接听方分成')->display(function ($data) {
                return "{$data}";
            });
            $grid->allowance('平台补贴费用')->display(function ($data) {
                return "{$data}";
            });
            $grid->serviceFee()->coin('平台服务费')->display(function ($data) {
                return "{$data}";
            });
            $grid->heartbeat_user('主叫方心跳更新时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', $time);
            });
            $grid->heartbeat_chat('被叫方心跳更新时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', $time);
            });
            // $grid->order_id('订单ID（接通才生成）');
            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('order_id', '话单ID');
                    $filter->equal('from', '话单类型')->select(VoiceCall::FROM_TYPE);
                    $filter->equal('channel_id', '频道ID');
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('fromUser.user_profile_id', '拨打方ID');
                    $filter->equal('status', '话单状态')->select(VoiceCall::STATUS);
                    $filter->equal('call_mark', '呼叫类型')->select([1 => '用户呼叫', 2 => '女用户呼叫']);
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('toUser.user_profile_id', '接听方ID');
                    $filter->between('created_at', '发起时间')->datetime();
                });
            });
            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableAll();
            });
        });
    }
}
